package dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.fragment_bill;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.ArrayList;

import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.R;

public class MyMarkerView extends MarkerView {
    private TextView tvContent;
    LineChart lineChart;
    ArrayList<String> xValues;

    public MyMarkerView(Context context,int layoutResource,LineChart lineChart,ArrayList<String> xValues){
        super(context,layoutResource);

        tvContent = findViewById(R.id.tvContent);
        this.lineChart = lineChart;
        this.xValues = xValues;
    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        LineData lineData = lineChart.getLineData();
        LineDataSet set1 = (LineDataSet)lineData.getDataSetByIndex(0);
        LineDataSet set2 = (LineDataSet)lineData.getDataSetByIndex(1);

        int DataSetIndex = highlight.getDataSetIndex();
        int index;
        if(DataSetIndex==0){
            index = set1.getEntryIndex(e);
        }
        else{
            index = set2.getEntryIndex(e);
        }
        Entry entry1 = set1.getEntryForIndex(index);
        Entry entry2 = set2.getEntryForIndex(index);

        tvContent.setText(xValues.get(index)+"月\n"+"支出："+entry1.getY()+"\n收入："+entry2.getY());
        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth()/2),-getHeight());
    }
}
